﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Grishko.DataTier.SqlLibrary;
using Grishko.DataTier;
using Grishko.DataTier.SqlSentence;

namespace OutdoorLib.BaseEntity.Contract
{
    /// <summary>
    /// График платежей
    /// </summary>
    public class PaymentsPlan : EntityList
    {
        public int Summa
        {
            get
            {

                int res = 0;
                foreach (PaymentsPlanItem i in this) { res = res + i.Summa; }
                return res;
            }
        }

        public PaymentsPlanItem[] GetPaymentsPlanItem()
        {
            //Grishko.DataTier.SqlSentence.ListWhere.TSortCondition SortCondition = new ListWhere.TSortCondition() { SortDirection = System.Web.UI.WebControls.SortDirection.Ascending, SortField = "Date" };
            //SortCondition.fnOrderBy = SortFunctions.fOrderBy;
            PaymentsPlanItem[] res = this.ToArray<PaymentsPlanItem>();
            //Array.Sort(res, SortCondition);
            return res;
        }
        
        public PaymentsPlanItem GetPrev(PaymentsPlanItem Current)
        {
            int i=-1;
            i = GetPosition(Current.ID);
            if (-1 == i) return null;            
            return GetPaymentsPlanItem()[(i==0?0:i-1)];
        }
        
        public PaymentsPlanItem GetNext(PaymentsPlanItem Current)
        {
            int i = -1;
            i = GetPosition(Current.ID);
            if (-1 == i) return null;
            return GetPaymentsPlanItem()[(i == 0 ? 0 : i + 1)];
        }
        
        /// <summary>
        /// Вернуть ближайший прошедший период для заданной даты
        /// </summary>
        /// <param name="Current"></param>
        /// <returns></returns>
        public PaymentsPlanItem GetPrev(DateTime Current)
        {
            PaymentsPlanItem[] s = GetPaymentsPlanItem();
            {
                for (int i = s.Length-1; i >-1 ; i--)
                {
                    if (s[i].Date < Current )
                        return s[i];
                }
            }
            return null;

        }

        private int GetPosition(string ID)
        {
            PaymentsPlanItem[] s = GetPaymentsPlanItem();
            for (int i=0 ;i<s.Length; i++ )
            {
                if (s[i].ID == ID)
                    return i;
            }

            return -1;
        
        }

        public double SummaForDate(DateTime dt)
        {
            PaymentsPlanItem[] Source = this.ToArray<PaymentsPlanItem>();

            var res =
            from p in Source
            where p.Date < dt || p.Date == dt
            select p.Summa;


            return res.Sum();





        }
    }

    /// <summary>
    /// Строчка плана выплат
    /// </summary>
    public class PaymentsPlanItem : EntitySingle
    {
        public DateTime Date { get; set; }
        public int Summa { get; set; }        
    }
}
